package com.mikiya.plugin;

import com.intellij.icons.AllIcons;
import com.mikiya.plugin.constant.PluginName;
import com.mikiya.plugin.dto.ClassInfoDTO;
import com.mikiya.plugin.dto.ClassMethodDTO;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.*;
import java.awt.datatransfer.StringSelection;

public class CopyableTreeCellRenderer extends DefaultTreeCellRenderer {
    private PluginName pluginName;

    public CopyableTreeCellRenderer(PluginName pluginName) {
        this.pluginName = pluginName;
    }

    public void copyToClipboard(String text) {
        StringSelection selection = new StringSelection(text);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection, null);
    }

    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected,
                                                  boolean expanded, boolean leaf, int row, boolean hasFocus) {
        super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);

        // 设置图标和文本等其他渲染属性
        // 根据节点的层级设置不同的图标
        if (value instanceof DefaultMutableTreeNode) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
            int level = node.getLevel();
            switch (level) {
                case 0:
                    setIcon(UIManager.getIcon("FileView.computerIcon"));
                    break;
                case 1:
                    ClassInfoDTO classInfo = (ClassInfoDTO) ((DefaultMutableTreeNode) value).getUserObject();
                    switch (classInfo.getClassType()) {
                        case ENUM:
                            setIcon(AllIcons.Nodes.Enum);
                            break;
                        case INTERFACE:
                            setIcon(AllIcons.Nodes.Interface);
                            break;
                        case ANNOTATION:
                            setIcon(AllIcons.Nodes.Annotationtype);
                            break;
                        default:
                            setIcon(AllIcons.Nodes.Class);
                            break;
                    }
                    break;
                case 2:
                    setIcon(AllIcons.Nodes.Method);
                    JPanel panel = new JPanel(new BorderLayout());
                    panel.add(this, BorderLayout.CENTER); // 单元格渲染器本身放置在面板的中心

                    ClassMethodDTO methodInfo = (ClassMethodDTO) ((DefaultMutableTreeNode) value).getUserObject();

                    if (PluginName.GLOBAL_RECYCLE.equals(pluginName)) {
                        if (methodInfo.getDel()) {
                            setForeground(Color.ORANGE); // 设置为橙色
                        }
                        // 添加标签到节点
                        JLabel label = new JLabel();
                        label.setText("" + methodInfo.getUsedCount());
                        panel.add(label, BorderLayout.LINE_END); // 标签放置在面板的右侧
                    }

                    if (PluginName.CYCLOMATIC_COMPLEXITY.equals(pluginName)) {
                        if (methodInfo.getCyclomaticComplexity() > 6) {
                            setForeground(Color.ORANGE); // 设置为橙色
                        }
                        // 添加标签到节点
                        JLabel label = new JLabel();
                        label.setText("" + methodInfo.getCyclomaticComplexity());
                        panel.add(label, BorderLayout.LINE_END); // 标签放置在面板的右侧
                    }

                    return panel;
                default:
                    break;
            }
        }

        return this;
    }
}
